<?php
namespace app\admin\controller;
use app\admin\controller\Base;

use think\Controller;
use think\Db;
class User extends Base
{

    
    // 会员列表
    public function userList()
    {

        $where = [];
        $where['admin_id'] = $this->accountId;
        $field = [];
        $res = db('user')->where($where)->field($field)->order('id desc')->paginate($this->page, false);
        //dump($res);exit;
       
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }

    // 会员列表 - 添加
    public function userAdd()
    {
        
        
        if (request()->isPost()) {
            
            $post = input('post.');
              //图片上传
            if ($_FILES['avatarurl']['tmp_name']) {
                // 获取表单上传文件 
                $file = request()->file('avatarurl');
                // 移动到框架应用根目录/public/uploads/ 目录下'
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                // $info = $file->move('/home/www/upload/');
                if($info){
                // 成功上传后 获取上传信息
                // 输出 
                // $img ='http://127.0.0.1/applet/'.'public' . DS . 'uploads' . '/' . $info->getSaveName();
                $avatarurl = 'uploads' . '/' . $info->getSaveName();
                $post['avatarurl'] = $avatarurl;
            }
        }

            $post['admin_id'] = $this->accountId;
            $post['create_time'] = time();
            $post['openid'] = md5($post['openid']);
            $post['admin_id'] = $this->accountId;
            $res = db('user')->insert($post);
                    if ($res) {
                        suc('','添加成功');
                    }else{
                        suc('','添加失败');
                    }
        }
        return $this->fetch();
    }

    // 会员列表 - 编辑
    public function userEdit($id)
    {

        if (request()->isPost()) {
            $post = input('post.');
              //图片上传
            if (!empty($_FILES['avatarurl']['tmp_name'])) {
                // 获取表单上传文件 
                $file = request()->file('avatarurl');
                // 移动到框架应用根目录/public/uploads/ 目录下'
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                // $info = $file->move('/home/www/upload/');
                if($info){
                // 成功上传后 获取上传信息
                // 输出 
                // $img ='http://127.0.0.1/applet/'.'public' . DS . 'uploads' . '/' . $info->getSaveName();
                $avatarurl = 'uploads' . '/' . $info->getSaveName();
                $data['avatarurl'] = $avatarurl;
            }
        }
            $data['nickname'] = $post['nickname'];
            $data['gender'] = $post['gender'];
            $data['country'] = $post['country'];
            $data['province'] = $post['province'];
            $data['city'] = $post['city'];
            $where = [];
            $where['id'] = $id;
            $where['admin_id'] = $this->accountId;
            $res = db('user')->where($where)->update($data);
                   if ($res) {
                        suc('','修改成功');
                    }else{
                        suc('','修改失败');
                    }
        }
        $res = db('user')->where(['id'=>$id])->find();
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 会员列表 - 删除
    public function userDel($id)
    {
        $where = [];
        $where['id'] = $id;
        $where['admin_id'] = $this->accountId;
        $res = db('user')->where($where)->delete();
        
        suc();
    }

    //图片上传
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('avatarurl');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            echo $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            echo $info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            echo $info->getFilename(); 
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }


    //查看收获地址
    public function examine()
    {   
        $id = input('id');
        $res = db('order_address')->where("admin_id='{$this->accountId}' and user_id='{$id}'")->order('id desc')->paginate($this->page, false);
        //dump($res);exit;
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }

}